package deal2;

import utils.SplitFileName;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SplitCsvByTime {
    public static void sbt(String path,int type){
//        String inputFilePath = "C:\\Users\\咚咚咚豆\\Desktop\\outSZ.csv";
        String path1=SplitFileName.sf(path);
        String csv1FilePath = path1+"1.csv";
        String csv2FilePath = path1+"2.csv";
        String systemDate = "2020-12-22"; // 你的系统日期

        SimpleDateFormat inputDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat outputDateFormat = new SimpleDateFormat("yyyy/MM-dd HH:mm:ss");

        try (BufferedReader br = new BufferedReader(new FileReader(path));
             FileWriter csv1Writer = new FileWriter(csv1FilePath);
             FileWriter csv2Writer = new FileWriter(csv2FilePath)) {

            // 读取CSV文件头部并写入两个新文件
            String header = br.readLine();
            csv1Writer.write(header + "\n");
            csv2Writer.write(header + "\n");

            String line;
            while ((line = br.readLine()) != null) {
                String[] data = line.split(",");
                String cksjString = data[type]; // CKSJ 列的值
                Date cksjDate = inputDateFormat.parse(cksjString);

                // 从系统时间提取小时和分钟
                String systemTime = new SimpleDateFormat("HH:mm:ss").format(new Date());

                // 格式化为 "2020/12-22 HH:mm:ss"
                String formattedCksj = systemDate + " " + systemTime;

                // 比较时间
                Date systemDateParsed = inputDateFormat.parse(formattedCksj);
                Date cksjParsed = inputDateFormat.parse(cksjString);

                if (cksjParsed.after(systemDateParsed)) {
                    csv2Writer.write(line + "\n");
                } else {
                    csv1Writer.write(line + "\n");
                }
            }

            System.out.println("Splitting completed. Data has been written to csv1 and csv2.");

        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }
    }
}
